Public Class frmVersionsManagement
    'form for managing aMod versions

#Region "Attributes"
#Region "Private attributes"
    Private MoDfrmVersionsManagement As ClsDfrmVersionsManagement = Nothing 'control object for this form
    Private MoAction As ClsAction = New ClsAction(GeAction.kkNoAction) 'action for this form
    Private MeResult As GeActionResult = GeActionResult.kkOk  'result of this form
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of form

    Private MiFocusedRowVersion As Integer = -1 'version of focused row
    Private MbFocusedRowEnabled As Boolean = False '"Enabled" column of focused row
    Private MoMarkedVersions As Collection = Nothing 'marked versions
    Private MoMarkedVersionsForOpen As Collection = Nothing 'marked versions for open
    Private MoMarkedVersionsForDelete As Collection = Nothing 'marked versions for delete
    Private MoMarkedVersionsForCompare As Collection = Nothing 'marked versions for compare
#End Region
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting form settings before its using
    Private Function InitControl() As GeActionResult
        SetReadyForUse(False) 'disabling form
        Try
            StartPosition = FormStartPosition.CenterParent
            MinimizeBox = False
            WindowState = FormWindowState.Normal
            Size = New Size(700, 400)
            MinimumSize = New Size(800, 500)
            ShowInTaskbar = False
            ShowIcon = True
            Icon = ConvertImageToIcon(imlFormImages.Images("FORMICON"))
            Text = "Sprava verzii modulu"

            AcceptButton = btnOpen
            CancelButton = btnCancel

            lblVersions.Text = "Verzie modulu ???"

            SetReadyForUse(True) 'enabling form
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
#End Region
#Region "Private properties"
    Private ReadOnly Property FocusedRow() As DevExpress.XtraTreeList.Nodes.TreeListNode
        Get
            Return ucnGridCard.tlTreeList.FocusedNode()
        End Get
    End Property
#End Region
#End Region
#Region "Events handler"
#Region "frmVersionsManagement - Events handler"
    Private Sub frmVersionsManagement_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        ucnGridCard.tlTreeList.SetFocusedNode(ucnGridCard.tlTreeList.GetNodeByVisibleIndex(0))
        SetEnable()
    End Sub
    Private Sub frmVersionsManagement_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'user closed via the forms control box close button (X) or via Me.Close - I set Me.DialogResult to Windows.Forms.DialogResult.OK when leaving form in successful situation
        If e.CloseReason = System.Windows.Forms.CloseReason.UserClosing AndAlso Me.DialogResult = Windows.Forms.DialogResult.Cancel Then
            MeResult = GeActionResult.kkCancel
        End If
        'since sometimes form is going to close and I dont know why, here is place for fixing the problem :-)
        Select Case e.CloseReason
            Case CloseReason.None
                e.Cancel = True
        End Select
    End Sub
#End Region
#Region "Buttons - Events handler"
    Private Sub btnCompareTwoVersions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompareTwoVersions.Click
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkaModTryToCompareVersions
        MoAction.SetData(MoDfrmVersionsManagement.DSaModFile.Path, "PATHTOFILE")
        MoAction.SetData(Math.Min(MoMarkedVersionsForCompare(1), MoMarkedVersionsForCompare(2)), "LEFTVERSION")
        MoAction.SetData(Math.Max(MoMarkedVersionsForCompare(1), MoMarkedVersionsForCompare(2)), "RIGHTVERSION")
        MeResult = Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkaModDeleteVersions
        MeResult = Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkaModTryToOpenVersions
        MeResult = Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub btnPromoteVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPromoteVersion.Click
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkamodPromoteVersion
        MoAction.SetData(MoDfrmVersionsManagement.DSaModFile.Path, "AMODPATHFORPROMOTING")
        MoAction.SetData(MiFocusedRowVersion, "VERSIONFORPROMOTING")
        MeResult = Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub barBtnExportModuleToXmod_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnExportModuleToXmod.ItemClick
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkaModExportModuleToXmod
        MoAction.SetData(MiFocusedRowVersion, "VERSIONFOREXPORT")
        Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub btnddExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnddExport.Click
        btnddExport.ShowDropDown()
    End Sub
    Private Sub barBtnImportModuleFromXmod_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnImportModuleFromXmod.ItemClick
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkaModImportModuleFromXmod
        MoAction.SetData(MoDfrmVersionsManagement.DSaModFile.Path, "AMODPATHFORIMPORT")
        MoAction.SetData(MiFocusedRowVersion, "VERSIONFORIMPORT")
        Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub btnddImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnddImport.Click
        btnddImport.ShowDropDown()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        MeResult = GeActionResult.kkCancel
        Me.Close()
    End Sub
#End Region
#Region "ucnGridCard - Events handler"
    Private Sub ucnGridCard_MMSettings() Handles ucnGridCard.MMSettings
        ucnGridCard.tlTreeList.OptionsView.ShowIndicator = False
        ucnGridCard.tlTreeList.OptionsSelection.EnableAppearanceFocusedCell = False
        ucnGridCard.tlTreeList.OptionsSelection.EnableAppearanceFocusedRow = True
        ucnGridCard.tlTreeList.OptionsView.ShowColumns = True
        ucnGridCard.tlTreeList.OptionsView.ShowCheckBoxes = True
        ucnGridCard.tlTreeList.OptionsView.ShowRoot = False
    End Sub
    Private Sub ucnGridCard_MMCreateDataSource() Handles ucnGridCard.MMCreateDataSource
        'ATTENTION: keep the same order here and during adding rows to DataSource
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(0).FieldName = "RowID"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(1).FieldName = "ParentRowID"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(2).FieldName = "Enabled"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(3).FieldName = "Selected"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(4).FieldName = "Version"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(5).FieldName = "VersionName"
        ucnGridCard.tlTreeList.Columns.Add()
        ucnGridCard.tlTreeList.Columns(6).FieldName = "Created"

        ucnGridCard.tlTreeList.Columns(4).VisibleIndex = 1
        ucnGridCard.tlTreeList.Columns(5).VisibleIndex = 2
        ucnGridCard.tlTreeList.Columns(6).VisibleIndex = 3
        '"RowID" AND "ParentRowID" ARE UNIQUE IDENTIFIERS FOR TREELIST AND THESE COLUMNS ARE INVISIBLE IN TREELIST COZ THEY ARE SET TO PROPERTIES KeyFieldName and ParentFieldName PROPERTIES OF TREELIST
    End Sub
    Private Sub ucnGridCard_MMColumnProperties(ByRef Column As DevExpress.XtraTreeList.Columns.TreeListColumn) Handles ucnGridCard.MMColumnProperties
        Try
            Select Case Column.FieldName
                Case "Version"
                    Column.Caption = "Verzia"
                    Column.OptionsColumn.AllowEdit = False
                    Column.SortOrder = SortOrder.Descending
                Case "Created"
                    Column.Caption = "Vytvorene"
                    Column.OptionsColumn.AllowEdit = False
                    Column.ColumnEdit = New DevExpress.XtraEditors.Repository.RepositoryItemTextEdit
                Case "VersionName"
                    Column.Caption = "Nazov verzie"
                    Column.OptionsColumn.AllowEdit = True
                Case Else
                    'Column.Caption will be the same like Column.FieldName
                    Column.VisibleIndex = -1
                    Column.OptionsColumn.AllowEdit = False
            End Select

        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ucnGridCard_MMFillDataSource(ByVal Item As Object, ByRef DataSource As Collection) Handles ucnGridCard.MMFillDataSource
        Try
            Dim LiI As Integer = 0
            Dim LbEnabled As Boolean = False
            For Each LoDataRow As DataRow In MoDfrmVersionsManagement.DSaModFile.GetVersionRowsAll
                LiI = LiI + 1
                LbEnabled = Not MoDfrmVersionsManagement.DSaModFile.OpenedEditableVersions.IsItemInCollection(LoDataRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VID)) AndAlso Not MoDfrmVersionsManagement.DSaModFile.OpenedReadOnlyVersions.IsItemInCollection(LoDataRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VID))
                DataSource.Add(New ClsUcnGridUnboundItemVersionsManagement(0 + LiI, -1, LbEnabled, False, LoDataRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VID), LoDataRow(AMod2.DsAMod.TableSysnames.Versions.Columns.VersionName), LoDataRow(AMod2.DsAMod.TableSysnames.Versions.Columns.Created)))
            Next
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ucnGridCard_MMCustomDrawNodeCell(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles ucnGridCard.MMCustomDrawNodeCell
        If Not e.Node("Enabled") Then
            e.Appearance.BackColor = Color.PaleVioletRed
        End If
    End Sub
    Private Sub ucnGridCard_MMFocusedRowChanged(ByVal NewRow As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal OldRow As DevExpress.XtraTreeList.Nodes.TreeListNode) Handles ucnGridCard.MMFocusedRowChanged
        SetEnable()
    End Sub
    Private Sub ucnGridCard_MMRowDoubleClicked(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode) Handles ucnGridCard.MMRowDoubleClicked
        Dim LoOriginalAction As GeAction = MoAction.Action
        MoAction.Action = GeAction.kkaModTryToOpenVersions
        MeResult = Execute(MoAction)
        MoAction.Action = LoOriginalAction
    End Sub
    Private Sub ucnGridCard_MMAfterCheckNode(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode) Handles ucnGridCard.MMAfterCheckNode
        SetEnable()
    End Sub
    Private Sub ucnGridCard_MMShowingEditor(ByVal e As System.ComponentModel.CancelEventArgs) Handles ucnGridCard.MMShowingEditor
        e.Cancel = Not (ucnGridCard.tlTreeList.FocusedColumn.FieldName = "VersionName" AndAlso ucnGridCard.tlTreeList.FocusedNode("Enabled"))
    End Sub
    Private Sub ucnGridCard_MMValueValidating(ByVal Item As Object, ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal Column As DevExpress.XtraTreeList.Columns.TreeListColumn, ByRef Value As Object, ByRef Valid As Boolean, ByRef ErrorText As String) Handles ucnGridCard.MMValueValidating
        Valid = ucnGridCard.tlTreeList.FocusedColumn.FieldName = "VersionName" AndAlso ucnGridCard.tlTreeList.FocusedNode("Enabled")
    End Sub
    Private Sub ucnGridCard_MMValueChanged(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal Column As DevExpress.XtraTreeList.Columns.TreeListColumn, ByVal NewValue As Object, ByVal OldValue As Object) Handles ucnGridCard.MMValueChanged
        MoDfrmVersionsManagement.DSaModFile.UpdateVersionName(Row("Version"), CStr(NewValue))
    End Sub
#End Region
#Region "ucnRTFeditorUC - Events handler"
    Private Sub ucnRTFeditorUC_MMValueChangesConfirmed(ByVal NewValue As String) Handles ucnRTFeditorUC.MMValueChangesConfirmed
        MoDfrmVersionsManagement.DSaModFile.UpdateRTFDescriptionOfVersion(MiFocusedRowVersion, ucnRTFeditorUC.ResultRTFText)
    End Sub
#End Region
#End Region
#Region "Functions"
    'gets all marked versions
    Private Function GetMarkedVersions() As Collection
        Dim LoMarkedVersions As Collection = New Collection

        For Each LoNode As DevExpress.XtraTreeList.Nodes.TreeListNode In ucnGridCard.tlTreeList.Nodes
            If LoNode.Checked Then LoMarkedVersions.Add(LoNode("Version"))
        Next

        Return LoMarkedVersions
    End Function
    'gets marked versions for open
    Private Function GetMarkedVersionsForOpen() As Collection
        Dim LoMarkedVersionsForOpen As Collection = GetMarkedVersions()

        If LoMarkedVersionsForOpen.Count = 0 Then
            If FocusedRow IsNot Nothing Then LoMarkedVersionsForOpen.Add(FocusedRow("Version"))
        End If

        Return LoMarkedVersionsForOpen
    End Function
    'gets marked versions for delete
    Private Function GetMarkedVersionsForDelete() As Collection
        Dim LoMarkedVersionsForDelete As Collection = New Collection

        For Each LoNode As DevExpress.XtraTreeList.Nodes.TreeListNode In ucnGridCard.tlTreeList.Nodes
            If LoNode.Checked AndAlso LoNode("Enabled") = True Then LoMarkedVersionsForDelete.Add(LoNode("Version"))
        Next

        If LoMarkedVersionsForDelete.Count = 0 Then
            If FocusedRow IsNot Nothing AndAlso FocusedRow("Enabled") Then LoMarkedVersionsForDelete.Add(FocusedRow("Version"))
        End If

        Return LoMarkedVersionsForDelete
    End Function
    'gets marked versions for compare
    Private Function GetMarkedVersionsForCompare() As Collection
        Dim LoMarkedVersionsForCompare As Collection = GetMarkedVersions()

        If LoMarkedVersionsForCompare.Count = 0 Then
            If FocusedRow IsNot Nothing Then LoMarkedVersionsForCompare.Add(FocusedRow("Version"))
        End If

        Return LoMarkedVersionsForCompare
    End Function
#End Region
#Region "Subs"
    'refreshes form and set enable/disable for controls
    Private Sub RefreshForm()
        ucnGridCard.RefreshDataSource()
        ucnGridCard.tlTreeList.SetFocusedNode(ucnGridCard.tlTreeList.GetNodeByVisibleIndex(0))
        SetEnable()
    End Sub
    'set enable/disable for controls
    Private Sub SetEnable()
        If FocusedRow IsNot Nothing Then
            MiFocusedRowVersion = FocusedRow("Version")
            MbFocusedRowEnabled = FocusedRow("Enabled")
        Else
            MiFocusedRowVersion = -1
            MbFocusedRowEnabled = False
        End If

        'fill collection of marked nodes
        MoMarkedVersions = GetMarkedVersions()
        'now according to collection MoMarkedVersions adjust collections of versions for open and delete
        MoMarkedVersionsForOpen = GetMarkedVersionsForOpen()
        MoMarkedVersionsForDelete = GetMarkedVersionsForDelete()
        MoMarkedVersionsForCompare = GetMarkedVersionsForCompare()

        btnCompareTwoVersions.Enabled = MoMarkedVersionsForCompare.Count = 2
        btnRemove.Enabled = MoMarkedVersionsForDelete.Count > 0
        btnOpen.Enabled = MoMarkedVersionsForOpen.Count > 0

        btnPromoteVersion.Enabled = FocusedRow IsNot Nothing
        btnddExport.Enabled = MoMarkedVersionsForOpen.Count > 0
        btnddImport.Enabled = FocusedRow IsNot Nothing

        ucnRTFeditorUC.ReadOnlyMode = IIf(FocusedRow Is Nothing, True, Not MbFocusedRowEnabled)
        If MiFocusedRowVersion <> -1 Then ucnRTFeditorUC.Init(MoDfrmVersionsManagement.DSaModFile.GetRTFdescriptionOfVersion(MiFocusedRowVersion))
    End Sub
    'sets MbReadyForUse attribute and sets Enable property of form
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling form
    End Sub
#End Region

    '#######################################
    'execute function
    Public Function Execute(ByRef OoAction As ClsAction) As GeActionResult
        Try
            MoAction = OoAction
            '---------------------------------------------
            'ACTION BEFORE ACTION
            '---------------------------------------------

            '---------------------------------------------
            'ACTION
            '---------------------------------------------
            Select Case MoAction.Action
                Case GeAction.kkaModVersionsManagement
                    MoDfrmVersionsManagement = MoAction.GetData("CLSDFRMVERSIONSMANAGEMENT")
                    lblVersions.Text = "Verzie modulu """ & MoDfrmVersionsManagement.DSaModFile.GetLastVersionModuleName & """"
                    ucnGridCard.Init(Nothing)
                    'SetEnable()

                    Me.ShowDialog()
                Case GeAction.kkaModTryToOpenVersions
                    MoAction.SetData(MoMarkedVersionsForOpen, "VERSIONS")
                    MeResult = MoDfrmVersionsManagement.Execute(MoAction)
                Case GeAction.kkaModDeleteVersions
                    If MessageBox.Show("Naozaj chcete odstranit oznacene verzie modulu? (pocet: " & MoMarkedVersionsForDelete.Count & ")", "Odstranenie verzii modulu", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        MoAction.SetData(MoMarkedVersionsForDelete, "VERSIONSFORDELETE")
                        MeResult = MoDfrmVersionsManagement.Execute(MoAction)
                    Else
                        MeResult = GeActionResult.kkCancel
                    End If
                Case GeAction.kkaModExportModuleToXmod
                    MeResult = MoDfrmVersionsManagement.Execute(MoAction)
                Case GeAction.kkaModImportModuleFromXmod
                    MeResult = MoDfrmVersionsManagement.Execute(MoAction)
                Case GeAction.kkamodPromoteVersion
                    MeResult = MoDfrmVersionsManagement.Execute(MoAction)
                Case GeAction.kkaModTryToCompareVersions
                    MeResult = MoDfrmVersionsManagement.Execute(MoAction)
            End Select
            '---------------------------------------------
            'ACTION AFTER ACTION
            '---------------------------------------------
            Select Case MoAction.Action
                Case GeAction.kkaModTryToOpenVersions
                    RefreshForm()
                Case GeAction.kkaModDeleteVersions, GeAction.kkaModImportModuleFromXmod, GeAction.kkamodPromoteVersion
                    If MeResult = GeActionResult.kkOk Or MeResult = GeActionResult.kkError Then
                        RefreshForm()
                    End If
            End Select
            '---------------------------------------------
            Return MeResult
            '---------------------------------------------

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    '#######################################

End Class